package com.itheima.ssm.controller;



import com.itheima.ssm.pojo.Item;
import com.itheima.ssm.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/item")
public class ItemController {

    ///我擦
    //注入业务仓
    @Autowired
    @Qualifier("itemServicessm")
    private ItemService itemService;
    //查询全部的方法
    @RequestMapping("/all")
    public ModelAndView findAll(ModelAndView modelAndView){
        List<Item> items = itemService.findAll();
        //  设置视图
        modelAndView.setViewName("list");
        //设置模型
        modelAndView.addObject("items",items);
        //转发到页面
        return modelAndView;
    }


    //模糊查询
    @RequestMapping(value = "/allName",produces = "application/json;charset=utf-8") //响应的乱码控制
    //@ResponseBody
    public ModelAndView findAllName(ModelAndView modelAndView,Item item){
        List<Item> items = itemService.findName(item);
        //  设置视图
        modelAndView.setViewName("listName");
        //设置模型
        modelAndView.addObject("items",items);
        //转发到页面
        return modelAndView;
    }


    //添加方法
    @RequestMapping("/insert")
    public String insertItem(Item item){
        itemService.insertItem(item);
        //重定向到显示页面
        return "redirect:item/all";
    }
}
